#include "luablock.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <QDebug>

LuaBlock::LuaBlock()
{
    m_type = ELuaTypeGenericBlock;
    m_parent = NULL;
    m_offset = 0;
    m_length = 0;    
}


LuaBlock::~LuaBlock()
{
    for(int i = 0; i < m_blocks.size(); i++)
        delete m_blocks[i];
}

LuaBlockTypeEnum LuaBlock::getType()
{
    return m_type;
}


void LuaBlock::addBlock(LuaBlock * aBlock)
{
    m_blocks.push_back(aBlock);
    aBlock->setParent(this);
}

void LuaBlock::print(int aIndent)
{
    QString indent = QString(aIndent, ' ');
    QString fullLine = indent + LuaBlock::getTypeLine(m_type);
    qDebug()<< fullLine << m_offset << m_length << "(" << m_name << ")";
    for(int i = 0; i < m_blocks.size(); i++)
        m_blocks[i]->print(aIndent + 1);
}

void LuaBlock::setParent(LuaBlock * aParent)
{
    m_parent = aParent;
}

LuaBlock * LuaBlock::getParent()
{
    return m_parent;
}

int LuaBlock::getOffset()
{
    return m_offset;
}

void LuaBlock::setLength(int aLength)
{
    m_length = aLength;
}

int LuaBlock::getLength()
{
    return m_length;
}

QVector<LuaBlock *> &LuaBlock::getBlocks()
{
    return m_blocks;
}

void LuaBlock::setType(LuaBlockTypeEnum aType)
{
    m_type = aType;
}

QString LuaBlock::getTypeLine(LuaBlockTypeEnum aType)
{
    switch(aType)
    {
    case ELuaTypeGenericBlock:
        return "ELuaTypeGenericBlock";
    case ELuaTypeChunk:
        return "ELuaTypeChunk";
    case ELuaTypeElseIf:
        return "ELuaTypeElseIf";
    case ELuaTypeElse:
        return "ELuaTypeElse";
    case ELuaTypeExpression:
        return "ELuaTypeExpression";
    case ELuaTypeFieldSelector:
        return "ELuaTypeFieldSelector";
    case ELuaTypeFor:
        return "ELuaTypeFor";
    case ELuaTypeBody:
        return "ELuaTypeBody";
    case ELuaTypeForList:
        return "ELuaTypeForList";
    case ELuaTypeForNumeric:
        return "ELuaTypeForNumeric";
    case ELuaTypeFunction:
        return "ELuaTypeFunction";
    case ELuaTypeFunctionName:
        return "ELuaTypeFunctionName";
    case ELuaTypeIf:
        return "ELuaTypeIf";
    case ELuaTypeIfThen:
        return "ELuaTypeIfThen";
    case ELuaTypeLocalFunction:
        return "ELuaTypeLocalFunction";
    case ELuaTypeLocalVariableDeclaration:
        return "ELuaTypeLocalVariableDeclaration";
    case ELuaTypeParameters:
        return "ELuaTypeParameters";
    case ELuaTypeRepeat:
        return "ELuaTypeRepeat";
    case ELuaTypeStatement:
        return "ELuaTypeStatement";
    case ELuaTypeWhile:
        return "ELuaTypeWhile";
    case ELuaTypeDoWhile:
        return "ELuaTypeDoWhile";
    case ELuaTypeAssignment:
        return "ELuaTypeAssignment";
    case ELuaTypeGlobalVariable:
        return "ELuaTypeGlobalVariable";
    case ELuaTypeLocalVariable:
        return "ELuaTypeLocalVariable";
    case ELuaTypeConstructor:
        return "ELuaTypeConstructor";
    case ELuaTypePrimaryExpression:
        return "ELuaTypePrimaryExpression";

    }
    return "unknown block";
}

void LuaBlock::setOffset(int aOffset)
{
    m_offset = aOffset;
}

QString LuaBlock::getName()
{
    return m_name;
}

void LuaBlock::setName(QString aName)
{
    m_name = aName;
}

LuaBlock *LuaBlock::getEnclosingBlock(int aOffset)
{
    //check childs first
    for(int i = 0; i < m_blocks.size(); i++)
    {
        LuaBlock * result = m_blocks[i]->getEnclosingBlock(aOffset);
        if(result)
            return result;
    }
    if(aOffset >= m_offset && aOffset < m_offset + m_length)
        return this;
    return NULL;
}



